#ifndef SERVER_H
#define SERVER_H

#include "connection.h"
#include "serverconnection.h"
#include "connectionplayer.h"
#include "packetdata.h"
#include "map.h"
#include "player.h"

class ServerConnection;

class Server : public Connection
{
    Q_OBJECT
public:
    Server(QHostAddress a, quint16 p, bool onlyReceive = false);
    Server(bool onlyReceive = false);
    void startServer(quint16 port);
    void stopServer();
    void sendToAll(PacketData* data, bool reliable = false,bool queue = true, QVector<ServerConnection*> *except = NULL);
    QHash<QString, ServerConnection*> getConnectionHash();
private:
    Map* map;
    QTimer *pingTimer;
    QHash<QString, ServerConnection*> connectionHash;
    qint32 idCount;

public slots:
    void onNewPacket(Packet*);


private slots:
    void broadcastPing();
    void timerTick();
    void onHitPlayer(Player*, Player*);
    void onConnectionStateChanged(QAbstractSocket::SocketState st);

signals:
    void newPlayerConnected(ConnectionPlayer* conn);
    void playerDisconnected(ConnectionPlayer* conn);
};

#endif // SERVER_H
